package cn.lili.modules.system.serviceimpl;

import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GrabOrderClient;
import cn.lili.modules.grab.dingdong.res.order.CreateOrderRequest;
import cn.lili.modules.system.constants.RequestLogTypeConstant;
import cn.lili.modules.system.entity.dos.RequestLog;
import cn.lili.modules.system.enums.RequestLogTypeEnum;
import cn.lili.modules.system.mapper.RequestLogMapper;
import cn.lili.modules.system.service.RequestLogService;
import cn.lili.mybatis.util.PageUtil;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * 第三方接口响应日志
 *
 * @author pyy
 * @since 2023/11/2
 */
@Service
public class RequestLogServiceImpl extends ServiceImpl<RequestLogMapper, RequestLog> implements RequestLogService {
    @Resource
    private GrabOrderClient grabOrderClient;

    @Override
    public boolean saveOrUpdate(RequestLog requestLog) {
        LambdaQueryWrapper<RequestLog> query = new LambdaQueryWrapper<>();
        query.eq(RequestLog::getItemTaskId, requestLog.getItemTaskId());
        RequestLog requestLogOfQuery = this.getOne(query);
        if (ObjectUtil.isNotNull(requestLogOfQuery)) {
            requestLog.setId(requestLogOfQuery.getId());
        }
        return super.saveOrUpdate(requestLog);
    }

    @Override
    public Page<RequestLog> getPage(PageVO pageVO) {
        return super.page(PageUtil.initPage(pageVO));
    }

    @Override
    public boolean editJsonParam(RequestLog requestLog) {
        if (Objects.isNull(requestLog.getId())) {
            throw new ServiceException("id不能为空。");
        }
        if (StringUtils.isEmpty(requestLog.getQueryString())) {
            throw new ServiceException("请求JSON不能为空。");
        }
        RequestLog requestLogOfQuery = this.getById(requestLog.getId());
        if (Objects.isNull(requestLogOfQuery)) {
            throw new ServiceException("该日志记录不存在。");
        }
        //当为企叮咚下单时，检查参数是否能转化为对应的实体类
        if (RequestLogTypeEnum.QI_DING_DONG_CREATE_ORDER.getKey().equals(requestLogOfQuery.getType())) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                mapper.readValue(requestLog.getQueryString(), CreateOrderRequest.class);
            } catch (Exception e) {
                throw new ServiceException("JSON转化失败，格式错误，请检查。");
            }
        }
        return this.updateById(requestLog);
    }

    @Override
    public ResultMessage<Object> reSend(Long id) {
        if (Objects.isNull(id)) {
            throw new ServiceException("该日志记录不存在。");
        }
        RequestLog requestLog = this.getById(id);
        if (Objects.isNull(requestLog)) {
            throw new ServiceException("该日志记录不存在。");
        }

        if (RequestLogTypeConstant.QI_DING_DONG_CREATE_ORDER.equals(requestLog.getType())) {
            CreateOrderRequest createOrderRequest;
            try {
                ObjectMapper mapper = new ObjectMapper();
                createOrderRequest = mapper.readValue(requestLog.getQueryString(), CreateOrderRequest.class);
            } catch (Exception e) {
                throw new ServiceException("JSON转化失败，格式错误，请检查。");
            }
            //调用企叮咚创建订单的接口
            String itemTaskId = Objects.isNull(requestLog.getItemTaskId()) ? null : String.valueOf(requestLog.getItemTaskId());
            createOrderRequest.setItemTaskId(itemTaskId);
            return grabOrderClient.retryCreateOrder(createOrderRequest);
        }
        throw new ServiceException("该请求不支持重发。");
    }

}
